package com.ddstation.app.invssettings.service;

import com.ddstation.app.invssettings.bean.SettingModifyBean;
import com.ddstation.common.util.ClassConvertUtil;
import com.ddstation.crm.institution.entity.DtHealthManageInstitutionEntity;
import com.ddstation.crm.institution.mapper.DtHealthManageInstitutionMapper;
import com.ddstation.crud.entity.DtInvsSettingsChangeRecordsEntity;
import com.ddstation.crud.entity.DtInvsTxtStorageEntity;
import com.ddstation.crud.mapper.DtInvsSettingsChangeRecordsMapper;
import com.ddstation.crud.mapper.DtInvsTxtStorageMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class InvsSettingsServiceImpl implements InvsSettingsService {

    private static final Logger LOGGER = LoggerFactory.getLogger(InvsSettingsServiceImpl.class);

    @Override
    public DtHealthManageInstitutionEntity getInvsSettings(String institutionId) {
        return dtHealthManageInstitutionMapper.getInvsSettingsByInstitutionId(institutionId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifySetting(SettingModifyBean modifyBean) {
        DtHealthManageInstitutionEntity entity = new DtHealthManageInstitutionEntity();
        entity.setId(modifyBean.getInstitutionId());
        PropertyDescriptor pd = null;
        try {
            pd = new PropertyDescriptor(modifyBean.getModifyProperty(), entity.getClass());
            Method setMethod = pd.getWriteMethod();//获得set方法
            if (null != setMethod) {
                setMethod.invoke(entity, modifyBean.getModifyValue());//此处为执行该Object对象的set方法
            }
        } catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
            LOGGER.error("配置注入错误，请确认修改字段是否正确。 " + e);
        }
        if (0 < dtHealthManageInstitutionMapper.update(entity)) {
            LOGGER.info("卫监设置修改成功。");
            DtInvsSettingsChangeRecordsEntity recordEntity = ClassConvertUtil.convertToTargetClass(modifyBean, DtInvsSettingsChangeRecordsEntity.class);
            return 0 < dtInvsSettingsChangeRecordsMapper.insert(recordEntity);
        } else {
            return false;
        }
    }

    @Override
    public DtInvsTxtStorageEntity getTxtByColumn(String column) {
        // 驼峰转下划线
        Pattern UpperCasePattern = Pattern.compile("[A-Z]");
        Matcher matcher = UpperCasePattern.matcher(column);
        String txtTag = column;
        StringBuffer sb = new StringBuffer("");
        while (matcher.find()) {
            //将当前匹配的子串替换成指定字符串，并且将替换后的子串及之前到上次匹配的子串之后的字符串添加到StringBuffer对象中
            //正则之前的字符和被替换的字符
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        //把之后的字符串也添加到StringBuffer对象中
        matcher.appendTail(sb);
        if (sb.length() == 0) {
            txtTag = column + "_promise";
        } else {
            txtTag = sb.toString() + "_promise";
        }
        return dtInvsTxtStorageMapper.selectByTag(txtTag);
    }

    @Inject
    private DtInvsSettingsChangeRecordsMapper dtInvsSettingsChangeRecordsMapper = null;
    @Inject
    private DtHealthManageInstitutionMapper dtHealthManageInstitutionMapper = null;

    @Inject
    private DtInvsTxtStorageMapper dtInvsTxtStorageMapper = null;
}
